package annotation.test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;

@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    AnnotationOperator ao = null;

    public DispatcherServlet() {
        super();
    }


    @Override
    public void init() throws ServletException {
        super.init();
        String root = getInitParameter("packagePath");
        String[] roots = root.split(",");
        System.out.println("================================" + Arrays.toString(roots));
        ao = new AnnotationOperator();//servlet创建时 执行一次
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getContextPath().equals("/FullTest")) {
            //request.getRequestDispatcher("/FullTest/AnnotationTest.jsp").forward(request, response);
            System.out.println("numA:" + request.getParameter("numA"));
            Enumeration<String> em = request.getParameterNames();
            String uri = request.getRequestURI();
            String method = uri.substring(uri.lastIndexOf("/") + 1, uri.indexOf(".do"));
            System.out.println(method);
            Object[] o = new Object[2];
            int i = 0;
            while (em.hasMoreElements()) {
                o[i] = request.getParameter(em.nextElement());
                i++;
            }
            ao.runMethod(method, o);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
